import axios from 'axios'
import { message } from 'antd'
import { getToken } from '../utils/user-token'
import { error } from 'console'
const instance = axios.create({
  timeout: 10 * 1000,
})
//  request  拦截器

instance.interceptors.request.use(
  config => {
    config.headers['Authorization'] = `Bearer ${getToken()}`
    return config
  },
  error => Promise.reject(error)
)
// 统一处理接口和处理信息
instance.interceptors.response.use(res => {
  const resData = (res.data || {}) as ResType

  const { errno, data, msg } = resData

  if (errno !== 0) {
    if (msg) {
      message.error(msg)
    }
    throw new Error(msg)
  }

  return data as any
})
export default instance

export type ResType = {
  errno: number
  data?: any
  msg?: string
}
export type ResDataType = {
  [key: string]: any
}
